package com.wu.framework.yearning.plus.starter.yearning.config;


import com.wu.framework.yearning.plus.starter.yearning.YearningApi;
import com.wu.framework.yearning.plus.starter.yearning.task.YearningTask;
import lombok.SneakyThrows;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.support.WebClientAdapter;
import org.springframework.web.service.invoker.HttpServiceProxyFactory;

/**
 * description 配置参数
 *
 * @author 吴佳伟
 * @date 2023/10/20 13:16
 */
@Configuration
@EnableScheduling
@Import(YearningTask.class)
@EnableConfigurationProperties(value = YearningConfig.class)
public class YearningClientEnableAutoConfiguration {

    @Bean
    public WebClient webClient(YearningConfig yearningConfig) {
        return WebClient.builder()
                .baseUrl(yearningConfig.getBaseUrl())
                .defaultHeader("ignoringLog", "true")
                .build();
    }

    @SneakyThrows
    @Bean
    public YearningApi yearningApi(WebClient webClient) {
        HttpServiceProxyFactory httpServiceProxyFactory =
                HttpServiceProxyFactory.builder(WebClientAdapter.forClient(webClient))
                        .build();
        return httpServiceProxyFactory.createClient(YearningApi.class);
    }

}
